<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Auth;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class FansController extends BaseController
{
    use AuthorizesRequests;

    public function __construct()
    {
        $this->middleware('auth');
    }

    public function fanUser(User $user)
    {
        $this->authorize('fan',$user);
        if(!Auth::user()->isFaning($user->id)){
            Auth::user()->fan($user->id);
        }
        return redirect()->route('users.show',$user->id);
    }

    public function unfanUser(User $user)
    {
        $this->authorize('fan',$user);
        if(Auth::user()->isFaning($user->id)){
            Auth::user()->unfan($user->id);
        }
        return redirect()->route('users.show',$user->id);
    }
}
